﻿
using HMS.DataContract.Request;
using HMS.DataContract.Response;
using HMS.Web.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Net.Mail;
using System.Web.Security;

namespace HMS.Web.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ValidateUser(string accessCode)
        {
            var client = InitializeSchedulerClient();
            //accessCode.Substring(0);
            UserInfo userInfo = client.ValidateUser(accessCode);
            //dynamic userInfo = new System.Dynamic.ExpandoObject();
            //userInfo.userId = userId;
            //userInfo.userType = "P";           
            client.Close();
            if (userInfo.Id > 0)
            {
                userInfo.ClinicCode = accessCode.Split('-')[0];
                Session["userInfo"] = userInfo;
                FormsAuthentication.SetAuthCookie(accessCode, true);
            }
            else
                FormsAuthentication.SignOut();
            //return Json(userId);
            return Json(userInfo);
        }

        /// <summary>
        /// Method to Initialize Scheduler Client
        /// </summary>
        /// <returns></returns>
        public SchedulerClient InitializeSchedulerClient()
        {

            var binding = new BasicHttpBinding();
            binding.SendTimeout = new TimeSpan(0, 0, 0, 0, 60000000);
            binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, 10000000);
            binding.MaxReceivedMessageSize = 10000000;
            binding.ReaderQuotas.MaxStringContentLength = 10000000;
            binding.ReaderQuotas.MaxDepth = 10000000;
            binding.ReaderQuotas.MaxArrayLength = 10000000;
            var endpointPath = ConfigurationManager.AppSettings["SchedulerEndpointPath"];
            var endpoint = new EndpointAddress(endpointPath);
            var client = new SchedulerClient(binding, endpoint);
            return client;
        }

        [HttpPost]
        public ActionResult SendEmail(string name, string from, string message)
        {
            Clinic clinic = new Clinic();
            if (Session["Clinic"] != null)
            {
                clinic = (Clinic)Session["Clinic"];
            }

            GenericFunction generic = new GenericFunction();
            string Body = "Name: " + name + "<br /> Email: " + from + "<br /> " + message;
            var result = generic.SendEmail(null, "Enquiry", Body, null, clinic.Code);
            return Json(result);
        }
      
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            Session.Abandon();
            //return View(
            return RedirectToAction("Index","Home");
        }

    }
}
